package com.gwtsz.chart.output.customer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.TextUtils;

import com.gwtsz.chart.renderer.YAxisRenderer;
import com.gwtsz.chart.utils.Transformer;
import com.gwtsz.chart.utils.Utils;
import com.gwtsz.chart.utils.ViewPortHandler;

/**
 * author：spence
 * mail：spenceacc@gmail.com
 * blog：http://blog.csdn.net/qqyanjiang
 * <p>
 * 重写y轴labels
 */
public class MyYAxisRenderer extends YAxisRenderer {
    protected MyYAxis mYAxis;

    public MyYAxisRenderer(ViewPortHandler viewPortHandler, MyYAxis yAxis, Transformer trans) {
        super(viewPortHandler, yAxis, trans);
        mYAxis = yAxis;
    }

    @Override
    protected void computeAxisValues(float min, float max) {
        /*只显示最大最小情况下*/
        if (mYAxis.isShowOnlyMinMaxEnabled()) {
            mYAxis.mEntryCount = 2;
            mYAxis.mEntries = new float[2];
            mYAxis.mEntries[0] = min;
            mYAxis.mEntries[1] = max;
            return;
        }
        /*折线图左边没有basevalue，则调用系统*/
        if (Float.isNaN(mYAxis.getBaseValue())) {
            super.computeAxisValues(min, max);
            return;
        }
        float base = mYAxis.getBaseValue();
        float yMin = min;
        int labelCount = mYAxis.getLabelCount();
        float interval = (base - yMin) / labelCount;
        int n = labelCount * 2 + 1;
        mYAxis.mEntryCount = n;
        mYAxis.mEntries = new float[n];
        int i;
        float f;
        for (f = min, i = 0; i < n; f += interval, i++) {
            mYAxis.mEntries[i] = f;
        }
    }

    @Override
    protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
        /*当有最小text的时候*/
        if (!TextUtils.isEmpty(mYAxis.getMinValue()) && mYAxis.isShowOnlyMinMaxEnabled()) {
            for (int i = 0; i < mYAxis.mEntryCount; i++) {
                /*获取对应位置的值*/
                String text = mYAxis.getFormattedLabel(i);
                if (i == 0) {
                    text = mYAxis.getMinValue();
                }
                if (i == 1) {
                    c.drawText(text, fixedPosition, mViewPortHandler.offsetTop() + 2 * offset + 5, mAxisLabelPaint);
                } else if (i == 0) {
                    c.drawText(text, fixedPosition, mViewPortHandler.contentBottom() - 3, mAxisLabelPaint);
                }
            }
        } else {
            for (int i = 0; i < mYAxis.mEntryCount; i++) {
                String text = mYAxis.getFormattedLabel(i);
                if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1)
                    return;

                int labelHeight = Utils.calcTextHeight(mAxisLabelPaint, text);
                float pos = positions[i * 2 + 1] + offset;

                if ((pos - labelHeight) < mViewPortHandler.contentTop()) {
                    pos = mViewPortHandler.contentTop() + offset * 2.5f + 8;
                } else if ((pos + labelHeight / 2) > mViewPortHandler.contentBottom()) {
                    pos = mViewPortHandler.contentBottom() - 3;
                } else {
                    if (mYAxis.isNeedOffset()) {
                        pos -= offset;
                    }
                }

                /** 指标颜色 */
                Paint paint = mAxisLabelPaint;
                if (i < (mYAxis.mEntryCount - 1) / 2) {
                    paint.setColor(getDecreasingColor());
                } else if (i > (mYAxis.mEntryCount - 1) / 2) {
                    paint.setColor(getIncreasingColor());
                } else {
                    paint.setColor(getNeutralColor());
                }
                /**
                 * 防止出现-0.00的情况（因为实现中存在无限循环小数导致）
                 */
                if (i == 3 && text.substring(0, 1).equals("-")) {
                    text = text.substring(1, text.length());
                }

                c.drawText(text, fixedPosition, pos, mAxisLabelPaint);
            }


        }
    }

    @Override
    public void renderGridLines(Canvas c) {

        if (!mYAxis.isEnabled())
            return;

        // pre alloc
        float[] position = new float[2];

        if (mYAxis.isDrawGridLinesEnabled()) {

            mGridPaint.setColor(mYAxis.getGridColor());
            mGridPaint.setStrokeWidth(mYAxis.getGridLineWidth());


            Path gridLinePath = new Path();

            // draw the horizontal grid
            for (int i = 0; i < mYAxis.mEntryCount; i++) {
                if (mYAxis.isSideNeedPathEffect()) {
                    mGridPaint.setPathEffect(mYAxis.getGridDashPathEffect());
                } else {
                    if (i == (mYAxis.mEntryCount - 1)) {
                        mGridPaint.setPathEffect(null);
                    } else {
                        mGridPaint.setPathEffect(mYAxis.getGridDashPathEffect());
                    }
                }

                position[1] = mYAxis.mEntries[i];
                mTrans.pointValuesToPixel(position);

                gridLinePath.moveTo(mViewPortHandler.offsetLeft(), position[1]);
                gridLinePath.lineTo(mViewPortHandler.contentRight(), position[1]);

                // draw a path because lines don't support dashing on lower android versions
                c.drawPath(gridLinePath, mGridPaint);

                gridLinePath.reset();
            }
        }

        if (mYAxis.isDrawZeroLineEnabled()) {

            // draw zero line
            position[1] = 0f;
            mTrans.pointValuesToPixel(position);

            drawZeroLine(c, mViewPortHandler.offsetLeft(), mViewPortHandler.contentRight(), position[1] - 1, position[1] - 1);
        }
    }
}
